﻿/*
Customizer by Hoai Thanh Trinh
*/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FShop_DAL;
using FShop_BAL;
public partial class ManageMember : System.Web.UI.Page 
{      
    protected void Page_Load(object sender, EventArgs e)
    {
        if ( ! IsPostBack )
        {
            BindDataGrid(1);
            //grdMember.DataSource = User_BAL.GetList_Member();
            //grdMember.DataBind();	
        }   
        else
        {
			
        } 
    }
	#region Bind data to grid
    protected void BindDataGrid(int pageIndex)
    {
		try
		{
			//grdMember.DataSource=User_BAL.Get_MemberAsPaging(pageIndex,15,"","*","","");        
            grdMember.DataSource = User_BAL.GetList_User();
			grdMember.DataBind();				
		}
		catch(Exception ex)
        {
			throw ex;
        }
		
    } 
	#endregion		
	
	#region Delete User
	protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
		try{
			string s = MemberSelectedArray();
			if (string.IsNullOrEmpty(s)) return;
            User_BAL.Delete_User_Where("User_ID In (" + s + ")");
			MessageBox.Show("Đã xóa thành công.");
			 BindDataGrid(1);
        }
        catch(Exception ex)
        {
			throw ex;
        }
    }
	//Lay ID tren cac cot duoc checked	
    private string MemberSelectedArray()
    {
        string strArrayID = "";
        foreach (GridViewRow rows in grdMember.Rows)
        {
            CheckBox chk = (CheckBox)rows.FindControl("chkCheck");

            if (chk.Checked)
            {
                ImageButton btnDel = (ImageButton)rows.FindControl("btnDelete");
                if (strArrayID != "")
                {
                    strArrayID += ",";
                }
                strArrayID += btnDel.CommandArgument.Trim();
            }
        }
        return strArrayID;
    }
	
	#endregion
	
	#region Datagrid events
	protected void grdMember_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdMember.PageIndex = e.NewPageIndex;
        BindDataGrid(grdMember.PageIndex);

    }
    protected void grdMember_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string strMemberID = e.CommandArgument.ToString().Trim();
        string strCommand = e.CommandName;                                   
        switch (strCommand)
        {             
            
            case "delete_Member":
                try
                {
                    User user = User_BAL.Get_User_ByID(int.Parse(strMemberID));
                    if (user == null) return;
                    User_BAL.Delete_User(user);
                    MessageBox.Show("Đã xóa thành công");
                    BindDataGrid(1);
                }
                catch(Exception ex)
				{
					throw ex;
				}
                break;
            case "active_Member":
                try
                {
                    User user = User_BAL.Get_User_ByID(int.Parse(strMemberID));
                    if (user == null) return;
                    user.User_Status = !user.User_Status;
                    User_BAL.Update_User(user);
                    MessageBox.Show("Đã kích hoạt thành công.");
                    BindDataGrid(1);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                break;
        }
        
    }
	
	protected void grdMember_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgActive = (ImageButton)e.Row.FindControl("btnActive");
            if (imgActive != null)
            {
                object obj = DataBinder.Eval(e.Row.DataItem, "User_Status");
                if (obj != null)
                {
                    if (obj.ToString().ToLower() == "true")
                    {
                        imgActive.ImageUrl = "Images/check.gif";
                    }
                    else
                    {
                        imgActive.ImageUrl = "Images/uncheck.gif";
                    }
                }
            }
        }
    }
	#endregion
	
	#region Extend events/codes
	
	#endregion
}


